﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using POCO = TrexSpace.POCO;
using Business = TrexSpace.Business;
namespace TrexSpace.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class CategoryServiceImpl : ICategoryServices
    {
        Business.CategoryManager _categoryManager = new Business.CategoryManager();

        [WebInvoke(Method = "GET",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "players")]

        public List<POCO.Category> GetCategories()
        {
            return ConvertToPOCO(_categoryManager.GetAllCategories());

        }

        public void AddCategory(string parentCatergory, string NewCategoryName)
        {
            _categoryManager.AddCategory(parentCatergory, NewCategoryName);
        }


        public POCO.Category GetCategory(string category)
        {
            return ConvertToPOCO(_categoryManager.GetAllCategories().Where(x => x.CategoryID.ToString() == category).First());
        }

        private POCO.Category ConvertToPOCO(Business.Category bCat)
        {
            POCO.Category oC = new POCO.Category();
            oC.CatrgoryID = bCat.CategoryID;
            oC.Name = bCat.Name;
            oC.ParentCategory = bCat.Parent;

            return oC;
        }
        private List<POCO.Category> ConvertToPOCO(List<Business.Category> ListbCat)
        {
            List<POCO.Category> lPocoCat = new List<POCO.Category>();
            foreach (var category in ListbCat)
            {
                lPocoCat.Add(ConvertToPOCO(category));
            }

            return lPocoCat;
        }

    }
}
